<?php

namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\AuthRule as AuthRuleModel;
use app\admin\model\AuthGroup as AuthGroupModel;
class ManagerGroup extends Base{
  public function initialize(){
    parent::initialize();
    $this->Model = new AuthGroupModel();
    $this->group_type = 'manager';
  }

  /**
   * 列表
   * @Author   zhanghong
   * @DateTime 2018-03-04
   * @return   [type]     [description]
   */
  public function index(){
    $param = $this->request->param();
    $res = $this->Model->adminPaginate($this->group_type, $param);
    $this->assign('param', $param);
    $this->assign('res', $res);
    return $this->fetch();
  }

  /**
   * 创建
   * @Author   zhanghong
   * @DateTime 2018-03-04
   * @return   [type]     [description]
   */
  public function create(){
    if($this->request->isPost()){
      $data = $this->getPostData();
      $save_res = $this->Model->saveItem($data);
      if($save_res['status']){
        $this->success($save_res['msg'], 'index');
      }else{
        $this->error($save_res['msg']);
      }
    }else{
      $MenuModel = new AuthRuleModel();
      $all_menu_tree = $MenuModel->findWithDescendants($this->group_type, 0, true);
      $this->assign('group_rules_id', []);
      $this->assign('group', ['status' => true]);
      $this->assign('all_menu_tree', $all_menu_tree);
      return $this->fetch('form');
    }
  }

  /**
   * 详细页
   * @Author   zhanghong
   * @DateTime 2018-03-04
   * @param    [type]     $id [description]
   * @return   [type]         [description]
   */
  public function read($id){
    $this->error('您访问的页面不存在', 'index');
  }

  /**
   * 编辑
   * @Author   zhanghong
   * @DateTime 2018-03-04
   * @param    [type]     $id [description]
   * @return   [type]         [description]
   */
  public function edit($id){
    if($this->request->isPost()){
      $data = $this->getPostData();
      $data['id'] = $id;
      $save_res = $this->Model->saveItem($data);
      if($save_res['status']){
        $this->success($save_res['msg'], 'index');
      }else{
        $this->error($save_res['msg']);
      }
    }else{
      $group = $this->Model->where('id', $id)->find();
      if(empty($group)){
        $this->redirect('index');
      }

      $MenuModel = new AuthRuleModel();
      $all_menu_tree = $MenuModel->findWithDescendants($this->group_type, 0, true);
      $group_rules_id = [];
      if(isset($group['rules'])){
        $group_rules_id = explode(',', $group['rules']);
      }
      $this->assign('group', $group);
      $this->assign('group_rules_id', $group_rules_id);
      $this->assign('all_menu_tree', $all_menu_tree);
      return $this->fetch('form');
    }
  }

  /**
   * 删除指定资源
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function delete($id){
    if($this->request->isDelete()){
      $del_res = $this->Model->deleteItem($id);
      if($del_res['status']){
        $this->success($del_res['msg'], 'index');
      }else{
        $this->error($del_res['msg']);
      }
    }else{
      $this->error('请求方式不正确', 'index');
    }
  }

  /**
   * 排序
   * @Author   zhanghong
   * @DateTime 2018-03-04
   * @return   [type]     [description]
   */
  public function resort(){
    $param = $this->request->param();
    $items = $param['items'];
    foreach ($items as $id => $sort_num) {
      $sort_num = intval($sort_num);
      if($sort_num < 0){
        $sort_num = 0;
      }else if($sort_num >= 9999){
        $sort_num = 9999;
      }
      $this->Model->save(['sort' => $sort_num], ['id' => $id]);
    }
    $this->redirect('index');
  }

  /**
   * 验证用户输入的title是否唯一
   * @Author   zhanghong
   * @DateTime 2018-03-04
   * @return   [type]     [description]
   */
  public function ajax_unique_title(){
    $data = $this->getPostData();
    if($this->Model->uniqueTitle($data)){
      echo('true');
    }else{
      echo('false');
    }
  }

  /**
   * 获取表单提交数据
   * @Author   zhanghong
   * @DateTime 2018-03-04
   * @return   [type]     [description]
   */
  private function getPostData(){
    $fields = [
      ['name' => 'id', 'type' => 'integer', 'default' => '', 'is_null' => true],
      ['name' => 'title', 'type' => 'string', 'default' => '', 'is_null' => false],
      ['name' => 'status', 'type' => 'boolean', 'default' => false, 'is_null' => false],
      ['name' => 'rules', 'type' => 'arr2str', 'default' => '', 'is_null' => false],
    ];
    $data = $this->parsePostParam($fields);
    return array_merge($data, ['group_type' => $this->group_type]);
  }
}
